package cn.aoyu.test.util;

import cn.aoyu.test.bean.user.User;
import cn.aoyu.test.controller.UserController;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/**
 * @ClassName: ResponseUtil
 * @Auther: xww
 * @Date: 2019/7/5 0005 10:58
 * @Description:
 */
public class ResponseUtil {

	protected static final Logger log = LoggerFactory.getLogger(ResponseUtil.class);

	public static void response(HttpServletRequest request, HttpServletResponse response, User user) {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setCharacterEncoding("UTF-8");
		String revcJson = JSON.toJSONString(user);
		response.setContentType("application/json; charset=utf-8");
		Writer out = null;
		try {
			out = response.getWriter();
			out.write(revcJson);
			out.flush();
		} catch (IOException e) {
			log.error("响应异常: {}", e.getMessage(), e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}
	}

}
